<?php

namespace App\Listeners\noti;

use App\Events\noti\Inform;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Http\Controllers\common\EmailController;
use App\Http\Controllers\common\VoiceController;
use App\Http\Controllers\common\YunpianController;

class InformListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  Inform  $event
     * @return void
     */
    public function handle(Inform $event)
    {
        $data = $event->data;
        $noti = $event->noti;
        $bemail = $event->users['bemail'];
        $bphone = $event->users['bphone'];
        $bmobile = $event->users['bmobile'];
        $user['area'] = $data['aname'].$data['rname'].$data['loc'];
        $user['num'] = $data['num'].'号';
        $content = $data['type'].'探测设备发生'.(($data['genre']=='告警')?$data['genre']:$data['genre'].'告警');
        $user['content']=$content.',请您及时检查并做好处理 !!';

        // 邮件
        if (($noti['2']['status']==1)&&$bemail) {
            foreach( $bemail as $k=>$v){
                if( !$v ) {
                    unset( $bemail[$k] );
                }
            }
            array_unique($bemail);
            $email['user'] = $bemail;
            $email['title'] = $data['type'].'告警通知！';
            $sendemail = new EmailController();
            $sendemail->sendEmail($email,$user);
        }

        // 短信
        if (($noti['0']['status']==1)&&$bphone) {
            foreach( $bphone as $k=>$v){
                if( !$v ) {
                    unset( $bphone[$k] );
                }
            }
            array_unique($bphone);
            $phone = implode($bphone,',');
            // $phone = '18568264580';
            $text = '尊敬的用户，您位于'.$user['area'].'的'.$user['num'].$content.'，请您及时检查并处置！';
            $yunpian = new YunpianController();
            $yunpian->Msgsend($phone,$text);
        }


        // 语音
        if (($noti['1']['status']==1)&&$bmobile) {
            foreach( $bmobile as $k=>$v){
                if( !$v ) {
                    unset( $bmobile[$k] );
                }
            }
            array_unique($bmobile);
            $mobile = implode($bmobile,',');
            $cnt = '105842;q:'.$user['area'].',w:'.$content;
            $voice = new VoiceController();
            $voice->sendMsg($mobile,$cnt);
        }
    }
}
